package com.qbm.association.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qbm.association.entity.Activity;
import com.qbm.association.entity.AdministratorS;
import com.qbm.association.mapper.AdministratorSMapper;
import com.qbm.association.service.AdministratorSService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Aron
 * @since 2021-12-12
 */
@Api(tags = "管理员控制层")
@RestController
@RequestMapping("/association/administrators")
public class AdministratorSController {
    @Autowired
    AdministratorSMapper administratorSMapper;
    @Autowired
    AdministratorSService administratorSService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<AdministratorS> getAdminList() {
        return administratorSMapper.selectList(null);
    }
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    public AdministratorS getAdmin(int id) {
        return  administratorSMapper.selectOne(new QueryWrapper<AdministratorS>().eq("id", id));
    }
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public String addAdmin(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        administratorSService.add(para);
        HashMap<String,Object> hs = new HashMap<>();
        hs.put("state",1);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String modifyAdmin(@RequestBody Map<String, Object> para) throws JsonProcessingException{
        administratorSService.modify(para);
        HashMap<String,Object> hs = new HashMap<>();
        hs.put("state",1);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String deleteAdmin(int id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        int i = administratorSMapper.deleteById(id);
        if (i != 0) {
            hs.put("state", 0);
        } else {
            hs.put("state", 1);
        }
        return objectMapper.writeValueAsString(hs);
    }

}

